var WindPosts = new Object();

WindPosts.createTableView = function() {
	var config = new Object();
	config.type = "post";
	config.tableTitle = "Posts";
	config.addTableColumns = function(tableModel) {
		{
			var column = new WindTableColumnModel();
			column.setTitle("Date");
			tableModel.addColumn(column);
		}
		{
			var column = new WindTableColumnModel();
			column.setTitle("User");
			tableModel.addColumn(column);
		}
		{
			var column = new WindTableColumnModel();
			column.setTitle("Message");
			tableModel.addColumn(column);
		}
	}
	config.addTableRowCellsFromEntity = function(row, entity) {
		row.addCell(entity.date);
		row.addCell(entity.user);
		row.addCell(entity.message);
	}
	config.dialogInsertTitle = "Insert a new post";
	config.dialogUpdateTitle = "Udate an existing post";
	config.configureDialogView = function(dialogView, entity) {
		
		var layout = WindLayout.createView();
		dialogView.getBody().addView(layout);
		
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("User:"); 
				var cell = row.createCellView();
				cell.addView(label);
			}
			
			dialogView.userText = WindText.createView();
			if(entity!=null) {
				dialogView.userText.setValue(entity.user);
			} else {
				dialogView.userText.setValue("Me");
			}
			var cell = row.createCellView();
			cell.addView(dialogView.userText);
		}
		
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("Date:"); 
				var cell = row.createCellView();
				cell.addView(label);
			}
			
			dialogView.publishedDate = WindDate.createView();
			if(entity!=null) {
				dialogView.publishedDate.setValue(entity.date);
			} else {
				dialogView.publishedDate.setValue(new Date());
			}
			var cell = row.createCellView();
			cell.addView(dialogView.publishedDate);
		}
		
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("Message:"); 
				var cell = row.createCellView();
				cell.addView(label);
			}
			
			dialogView.messageText = WindText.createView();
			dialogView.messageText.setWidth(350);
			dialogView.messageText.setLines(5);
			if(entity!=null) {
				dialogView.messageText.setValue(entity.message);
			} else {
				dialogView.messageText.setValue("Test");
			}
			var cell = row.createCellView();
			cell.addView(dialogView.messageText);
		}
	}
	config.fillEntityFromDialog = function(dialogView, entity) {
		entity.user = dialogView.userText.getValue();
		entity.date = dialogView.publishedDate.getValue();
		entity.message = dialogView.messageText.getValue();
	}
	
	var tableView = WindEntities.createTableViewFromConfig(config);
	return tableView;
}

WindPosts.createView = function() {
	var postsView = new WindView();
	postsView.setCssClass("WindPosts");
	
	var cmd = new Object();
	cmd.operation = "datastore.list";
	cmd.type = "post";
	var response = WindData.execute(cmd);
	
	for(var i=0; i<response.rows.length; i++) {
		var post = response.rows[i];
		var postView = WindPosts.createPostView(post);
		postsView.addView(postView);
	}
	
	return postsView;
}

WindPosts.createPostView = function(post) {
	var postView = new WindView();
	postView.setCssClass("WindPost");
	
	var userView = new WindView();
	userView.setCssClass("WindPostUser");
	userView.setText(post.user);
	postView.addView(userView);

	var dateView = new WindView();
	dateView.setCssClass("WindPostDate");
	dateView.setText(post.date);
	postView.addView(dateView);

	var messageView = new WindView();
	messageView.setCssClass("WindPostMessage");
	messageView.setText(post.message);
	postView.addView(messageView);
	
	return postView;
}

